package com.nfit.shopweb.web.api;

import com.github.pagehelper.PageInfo;
import com.nfit.shopmodel.dto.product.CategoryBrandDto;
import com.nfit.shopmodel.entity.product.Brand;
import com.nfit.shopmodel.entity.product.CategoryBrand;
import com.nfit.shopmodel.vo.common.Result;
import com.nfit.shopmodel.vo.common.ResultCodeEnum;
import com.nfit.shopweb.service.BrandService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:BrandController
 * Package:com.nfit.shopweb.web.api
 * Description
 *
 * @Author L
 * @Create 2024/12/11 19:06
 */
@Slf4j
@Tag(name = "商品品牌")
@RestController
@RequestMapping("/api/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/selectAll/{pageNum}/{pageSize}")
    public Result selectAll(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize){
        PageInfo<List<Brand>> pageInfo = brandService.selectAll(pageNum, pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询所有的品牌数据
     */
    @GetMapping("/list")
    public Result list(){
       List<Brand> list = brandService.list();
       return Result.build(list,ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据三级商品分类id查询对应商品分类下对应的品牌数据
     * @param category3Id 三级商品分类id
     */
    @GetMapping("/findByCategory3Id/{category3Id}")
    public Result findByCategory3Id(@PathVariable("category3Id") Long category3Id){
        List<Brand> list = brandService.findByCategory3Id(category3Id);
        return Result.build(list,ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/save")
    public Result save(@RequestBody Brand brand){
        brandService.save(brand);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Brand brand){
        brandService.update(brand);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        brandService.delete(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询分类品牌 所对应的商品分类和品牌
     */
    @PostMapping("/selectCategoryBrand/{pageNum}/{pageSize}")
    public Result selectCategoryBrand(@PathVariable("pageNum") Integer pageNum,
                                      @PathVariable("pageSize") Integer pageSize,
                                      @RequestBody(required = false) CategoryBrandDto CategoryBrandDto){
       PageInfo<List<CategoryBrand>> list = brandService.selectCategoryBrand(pageNum,pageSize,CategoryBrandDto);
       return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除分类品牌
     * @param id 分类品牌id
     */
    @DeleteMapping("/deleteCategoryBrand/{id}")
    public Result deleteCategoryBrand(@PathVariable("id") Long id){
        log.info("delete category brand {}", id);
        brandService.deleteCategoryBrand(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 添加分类品牌
     * @param CategoryBrand 分类品牌实体
     */
    @PostMapping("/saveCategoryBrand")
    public Result saveCategoryBrand(@RequestBody CategoryBrand CategoryBrand){
        brandService.saveCategoryBrand(CategoryBrand);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改分类品牌
     *前端有问题
     */
    @PutMapping("/updateCategoryBrand")
    public Result updateCategoryBrand(@RequestBody CategoryBrand CategoryBrand){
        brandService.updateCategoryBrand(CategoryBrand);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

}
